#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QCheckBox>

// static int checkCount = 0; // 勾选的checkBox的数量

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 设置CheckBox 三态属性
    ui->CarBrand->setTristate(true);

    connect(ui->CarBrand, &QCheckBox::clicked, this, [=](bool cbState)
    {
        if(cbState) // 如果主复选框（车企品牌）被选中
        {
            // 复选框全部设为 选中状态
            ui->Audi->setChecked(true);
            ui->Ferrari->setChecked(true);
            ui->Ford->setChecked(true);
            ui->Lamborghini->setChecked(true);
            ui->Mercedes_Benz->setChecked(true);
            ui->Red_Flag->setChecked(true);
            ui->Toyota->setChecked(true);
        }
        else // 复选框未被选中
        {
            // 复选框设为 非选中状态
            ui->Audi->setChecked(false);
            ui->Ferrari->setChecked(false);
            ui->Ford->setChecked(false);
            ui->Lamborghini->setChecked(false);
            ui->Mercedes_Benz->setChecked(false);
            ui->Red_Flag->setChecked(false);
            ui->Toyota->setChecked(false);
        }
    });

    // 连接复选框 与 状态改变函数
    connect(ui->Audi, &QCheckBox::stateChanged, this, &MainWindow::statusChanged);
    connect(ui->Ferrari, &QCheckBox::stateChanged, this, &MainWindow::statusChanged);
    connect(ui->Ford, &QCheckBox::stateChanged, this, &MainWindow::statusChanged);
    connect(ui->Lamborghini, &QCheckBox::stateChanged, this, &MainWindow::statusChanged);
    connect(ui->Mercedes_Benz, &QCheckBox::stateChanged, this, &MainWindow::statusChanged);
    connect(ui->Red_Flag, &QCheckBox::stateChanged, this, &MainWindow::statusChanged);
    connect(ui->Toyota, &QCheckBox::stateChanged, this, &MainWindow::statusChanged);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::statusChanged(int state)
{
    if(state == Qt::Checked) // 每有一个CheckBox被勾选，则++checkCount
        ++checkCount;
    else
        --checkCount;

    // 通过勾选次数 设置主复选框状态
    if(checkCount == 7)
        ui->CarBrand->setCheckState(Qt::Checked);
    else if(checkCount == 0)
        ui->CarBrand->setCheckState(Qt::Unchecked);
    else
        ui->CarBrand->setCheckState(Qt::PartiallyChecked);
}
